<!-- 弹窗 详情 -->
<script lang="tsx" setup>
import FileUpload from "@/components/file-system/file-upload.vue";
import type { FormDescChild } from "@/components/form-desc/form-desc.vue";
import { whManagerBsTbRiskHazardsHandleSupervisionUsingPost } from "@/genapi/wh";
import { useAsync } from "@/hooks/use-async";
import {
	whManagerBsTbRiskHazardsControlGetByIdIdUsingPost,
	whManagerBsTbEnterpriseGetListUsingPost,
} from "@/genapi/wh";
import { computedLoading } from "@/hooks/computed-loading";
import { HAZARD_LEVEL_OPTIONS } from "@/config/enum-map";

import { injectState } from "../state";

defineOptions({ name: "DetailDialog" });

const { page } = injectState()!;

const dialogRef = shallowRef<InstanceType<GlobalComponents["PromiseDialog"]>>();
const formDescRef = shallowRef<InstanceType<GlobalComponents["FormDesc"]>>();

const type = ref<"write" | "read">();

const beforeClose = async () => {
	if (type.value === "write") {
		await ElMessageBox.confirm("确认关闭？");
	}
};

const trigger = async (_type: "write" | "read" = "read", value = {} as any) => {
	type.value = _type;
	model.value = JSON.parse(JSON.stringify(value)) as any;
	await dialogRef.value?.open();
};

defineExpose({
	trigger,
});

const model = ref<(typeof page.records.value)[0]>({});

const file = computed({
	get() {
		if (model.value.riskHazardsIdList) {
			return [
				{
					id: model.value.fileId,
					name: model.value.fileName,
				},
			];
		}
	},
	set(value: any) {
		model.value.riskHazardsIdList = [];
		value.forEach((item) => {
			model.value.riskHazardsIdList.push(item.id);
		});
	},
});

const title = computed(() => {
	if (type.value === "write") {
		return model.value.id ? "编辑" : "新增";
	} else {
		return `详情`;
	}
});

const { execute, isLoading } = useAsync(async () => {
	await formDescRef.value?.formRef?.validate();
	await ElMessageBox.confirm("确认提交？", "提示", {
		type: "warning",
	});

	await whManagerBsTbRiskHazardsHandleSupervisionUsingPost({
		data: {
			...model.value,
		},
	});
	ElMessage.success("提交成功");
	page.execute();
	dialogRef.value?.complete();
});

const [state] = computedLoading(async () => {
	const { data } = await whManagerBsTbRiskHazardsControlGetByIdIdUsingPost({
		path: { id: model.value.id },
	});
	return data;
});

const [companyList] = computedLoading(async () => {
	const { data } = await whManagerBsTbEnterpriseGetListUsingPost({ data: {} });
	return data?.map((item) => ({ label: item.name, value: item.id })) ?? [];
});
console.log(companyList)
const getCompanyName = () => {
	const res = companyList.value.filter((item) => {
    // console.log(model.value.enterpriseId)
		return item.id === model.value.enterpriseId;
	});
	return res[0] ? res[0].label : "--";
};

const children: FormDescChild[] = [
	{
		label: "督办企业",
		prop: "enterpriseId",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-select-v2
				class="w-100%"
				options={companyList ?? []}
				v-model={model.value!.enterpriseId}
				placeholder="请选择"
			/>
		),
		descItem: () => `${model.value.enterpriseId ?? "-"}`,
	},
	{
		label: "隐患级别",
		prop: "level",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-select-v2
				class="w-100%"
				options={[...HAZARD_LEVEL_OPTIONS]}
				v-model={model.value!.level}
				placeholder="请选择"
			/>
		),
		descItem: () => `${model.value.level ?? "-"}`,
	},
	{
		label: "整改开始日期",
		prop: "handlePlanStartDate",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-date-picker
				v-model={model.value.handlePlanStartDate}
				clearable
				type="date"
				class="w-100%"
				placeholder="开始日期"
				value-format="YYYY-MM-DD"
			/>
		),
		descItem: () => `${model.value.handlePlanStartDate ?? "-"}`,
	},
	{
		label: "整改结束日期",
		prop: "handlePlanEndDate",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-date-picker
				v-model={model.value.handlePlanEndDate}
				clearable
				type="date"
				class="w-100%"
				placeholder="开始日期"
				value-format="YYYY-MM-DD"
			/>
		),
		descItem: () => `${model.value.handlePlanEndDate ?? "-"}`,
	},
	{
		label: "经度",
		prop: "longitude",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.longitude} placeholder="请输入" />
		),
		descItem: () => `${model.value.longitude ?? "-"}`,
	},
	{
		label: "纬度",
		prop: "latitude",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.latitude} placeholder="请输入" />
		),
		descItem: () => `${model.value.latitude ?? "-"}`,
	},
	{
		label: "隐患描述",
		prop: "description",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 2,
		formItem: () => (
			<el-input
				type="textarea"
				rows={2}
				v-model={model.value.description}
				placeholder="请输入"
			/>
		),
		descItem: () => `${model.value.description ?? "-"}`,
	},
	{
		label: "隐患图片",
		prop: "riskHazardsIdList",
		rule: [{ required: false, message: "请输入", trigger: "blur" }],
		span: 2,
		formItem: () => (
			<FileUpload v-model={file.value} limit={3} w="100%">
				<el-button type="primary">
					{{
						icon: () => <el-icon class="i-custom:add"></el-icon>,
						default: () => "增加附件",
					}}
				</el-button>
			</FileUpload>
		),
		descItem: () => (
			<FileUpload v-model={file.value} limit={3} disabled={true} />
		),
	},
];
</script>

<template>
	<PromiseDialog
		ref="dialogRef"
		:title="title"
		:before-close="beforeClose"
		w="707px!"
	>
		<el-scrollbar h="408px!">
			<el-descriptions title="隐患描述">
				<el-descriptions-item :span="2" label="所属企业">{{
					getCompanyName()
				}}</el-descriptions-item>
				<el-descriptions-item :span="2" label="上报时间">{{
					model.reportTime
				}}</el-descriptions-item>
				<el-descriptions-item :span="2" label="上报人员">{{
					model.reporterName
				}}</el-descriptions-item>
				<el-descriptions-item :span="2" label="联系方式">{{
					model.reporterPhoneNo
				}}</el-descriptions-item>
				<el-descriptions-item :span="2" label="隐患描述">{{
					model.description
				}}</el-descriptions-item>
				<el-descriptions-item :span="2" label="隐患位置">{{
					`${model.longitude},${model.latitude}`
				}}</el-descriptions-item>
				<el-descriptions-item :span="2" label="隐患图片">
					--
				</el-descriptions-item>
			</el-descriptions>
			<FormDesc
				ref="formDescRef"
				m="t-24px x-0px"
				v-if="type === 'write'"
				:type="type === 'read' ? 'desc' : 'form'"
				form-class="gap-x-20px"
				:model="model"
				:children="children"
				:column="2"
			/>
		</el-scrollbar>
		<template v-if="type === 'write'" #footer>
			<el-button @click="dialogRef?.close()">取消</el-button>
			<el-button type="primary" :is-loading="isLoading" @click="execute()"
				>确定</el-button
			>
		</template>
	</PromiseDialog>
</template>
